<template>
  <div class="login-container">
    <h2>登录</h2>
    <form @submit.prevent="login">
      <div class="form-control">
        <label for="username">用户名:</label>
        <input type="text" id="" v-model="username" required>
      </div>
      <div class="form-control">
        <label for="password">密码:</label>
        <input type="password" id="password" v-model="password" required>
      </div>
      <button type="submit" class="login-btn">登录</button>
      <router-link to="/register" class="register-link">没有账号？去注册</router-link>
    </form>
    <p v-if="error" class="error-message">{{ error }}</p>
  </div>
</template>

<script>
import axios from 'axios';
axios.defaults.baseURL="http://localhost:8087"
export default {
  data() {
    return {
      userid:null,
      username: '',
      password: '',
      error: ''
    };
  },
  methods: {
    login() {
      // 登录逻辑，可能包含验证和发送请求到后端
        axios.get(`/findUserByName/${this.username}`).then(res=>{
          if(res.data)
          {
            console.log(res.data)
            this.userid=res.data.userid
            sessionStorage.setItem("userid",this.userid);
            alert('登录成功');
            this.username='';
            this.password= '',
            this.error= ''
            this.$router.push({
                    name:'food',
                })
          }
          else{
            alert('登录失败');
          }
        })
        
    }
  }
}
</script>

<style scoped>
.login-container {
  width: 100%;
  max-width: 400px;
  margin: 30px auto;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  color: #333;
}

.form-control {
  margin-bottom: 15px;
  position: relative;
}

.form-control label {
  display: block;
  margin-bottom: 5px;
}

.form-control input {
  width: 100%;
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.login-btn {
  display: block;
  width: 100%;
  padding: 10px;
  border: none;
  background-color: #007bff;
  color: white;
  border-radius: 4px;
  cursor: pointer;
}

.login-btn:hover {
  background-color: #0056b3;
}

.error-message {
  color: red;
  text-align: center;
  margin-top: 5px;
}

/* 响应式设计 */
@media (max-width: 600px) {
  .login-container {
    padding: 15px;
  }
}
</style>
